package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.db.Authentication;
import com.apiit.webserviceclient.db.Authentication_DAO;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PagolLecturerLogin extends Activity implements OnClickListener {
	
	private Authentication_DAO datasource1;
	EditText username;
	EditText pass;
	Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pagol_lecturer_login);
		
		final Intent i = new Intent(this, LecturerPagol.class);
		username = (EditText) findViewById(R.id.editTxtPagolusername);
		pass = (EditText) findViewById(R.id.EditTxtPagolpassword);
		button = (Button) findViewById(R.id.btnPagolLogin);
		button.setOnClickListener(this);

		datasource1 = new Authentication_DAO(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pagol_lecturer_login, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == button) {
			datasource1.open();
			try {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/login.asp");

			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put(
					"SID",
					"/html[1]/body[1]/div[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[1]/input[2]---value");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;

				respones = bpelProcess.doRequest(request);


			BPEL_Request request2 = new BPEL_Request();
			String url_lib_auth = "http://webspace1.apiit.lk/db_authentication/login.asp?REDIRECTPAGE=http://webspace1.apiit.lk/pagol15/www/login.asp&SID="
					+ respones.getResultsMap().get("SID");
			request2.setUrl(url_lib_auth);

			Map<String, String> xpathMap2 = new HashMap<String, String>();
			xpathMap2.put("title", "//title---");
			request2.setXpathMap(xpathMap2);
			Map<String, String> parametersMap2 = new HashMap<String, String>();

			parametersMap2.put("txtUserName", username.getText().toString());
			parametersMap2.put("txtPassword", pass.getText().toString());
			parametersMap2.put("REDIRECTPAGE",
					"http://webspace1.apiit.lk/pagol15/www/login.asp");
			parametersMap2.put("SID", respones.getResultsMap().get("SID"));
			parametersMap2.put("Submit", "Login");

			request2.setParametersMap(parametersMap2);
			request2.setType("POST");
			BPEL_Response respones2 = null;

				respones2 = bpelProcess.doRequest(request2);

			BPEL_Process bpelProcess3 = new BPEL_Process();
			BPEL_Request request3 = new BPEL_Request();
			request3.setUrl("http://webspace1.apiit.lk/pagol15/www/lecturer_top.asp");

			Map<String, String> xpathMap3 = new HashMap<String, String>();
			xpathMap3.put("title", "/html[1]/body[1]/table[2]/tr[1]/td[1]/table[1]/tr[2]/td[1]/table[1]/tr[1]/td[1]/table[1]/tr[2]/td[2]---");
			request3.setXpathMap(xpathMap3);
			
			Map<String, String> parametersMap3 = new HashMap<String, String>();
			request3.setParametersMap(parametersMap3);
			
			request3.setType("GET");
			BPEL_Response respones3 = null;
			respones3 = bpelProcess3.doRequest(request3);


			String testResult = respones3.getResultsMap().get("title");

			Toast.makeText(getApplicationContext(), testResult,
					Toast.LENGTH_LONG).show();

			if (respones3.getStatus().equalsIgnoreCase(GlobalData.message_success)) {
				if (datasource1.getAction(GlobalData.authentication_id_pagolLec_perUser) == null) {
					Authentication authenticaton = new Authentication();
					authenticaton.setAuthentication_id(GlobalData.authentication_id_pagolLec_perUser);
					authenticaton.setauthentication_name(GlobalData.authentication_name_pagolLec);
					authenticaton.setuser_name(username.getText().toString());
					authenticaton.setpassword(pass.getText().toString());
					datasource1.createAction(authenticaton);
				}
				else
				{
					Authentication authenticaton = new Authentication();
					authenticaton.setuser_name(username.getText().toString());
					authenticaton.setpassword(pass.getText().toString());
					datasource1.updateAuthenticationDetails(GlobalData.authentication_id_pagolLec_perUser, GlobalData.authentication_name_pagolLec,username.getText().toString(), pass.getText().toString());
					
				}

				datasource1.close();
				Intent i = new Intent(this, LecturerPagol.class);
				i.putExtra("title", testResult);
				startActivity(i);

			}else{
				
				AlertDialog.Builder alt = new AlertDialog.Builder(this);
				alt.setTitle("Error...");
				alt.setMessage("The username or password you entered is incorrect");
				alt.setNeutralButton("Close", null);
				alt.show();
				Intent i = new Intent(this, Login_Activity.class);
				startActivity(i);
			}

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
